<template>
  <el-dialog width="500px" :title="title" :visible.sync="dialogFormVisible">
    <div class="formCenter">
      <el-form>
        <el-form-item label="角色名称：" :label-width="formLabelWidth">
          <el-input class="el_input" size="medium" v-model="roleName" placeholder="请输入"></el-input>
        </el-form-item>

        <el-form-item label="角色描述：" :label-width="formLabelWidth">
          <el-input class="el_input" size="medium" v-model="roleDescribe" placeholder="请输入"></el-input>
        </el-form-item>
      </el-form>
    </div>

    <div slot="footer" class="dialog-footer">
      <el-button size="small" @click="dialogFormVisible = false">取 消</el-button>
      <el-button size="small" type="primary" @click="confirmModifData">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import HttpInterface from '../../util/httpInterface'

export default {
  components: {},
  data() {
    return {
      title: "添加角色信息",
      dialogFormVisible: false,
      formLabelWidth: "85px",
      roleName: "",
      roleDescribe: "",
      id: 0
    };
  },

  methods: {
    confirmModifData() {
      if (this.id == 0) {
        this.addRoleInfo();
      } else {
        this.modifRoleInfo(this.id);
      }
    },

    // 添加信息
    addRoleInfo () {
      let _this = this;
      let params = {
        rolename: this.roleName,
        roledesc: this.roleDescribe
      }
      HttpInterface.requestPost(this, HttpInterface.URL_ROLE_ADD_ROLE, params, function (response) {
        var success = response.data.success;
        if (success) {
          _this.dialogFormVisible = false;
          _this.$parent.onRefreshClick();
        }
      }, null, true)
    },

    getRoleInfo (id) {
      let _this = this;
      let params = {
        id: id
      }
      HttpInterface.requestPost(this, HttpInterface.URL_ROLE_EDIT_SHOW, params, function (response) {
        var data = response.data.other;
        _this.roleName = data.rolename;
        _this.roleDescribe = data.roledesc;
      }, null, true)
    },

    // 编辑信息
    modifRoleInfo (id) {
      let _this = this;
      let params = {
        editid : id,
        ename: this.roleName,
        ecomment: this.roleDescribe
      }
      HttpInterface.requestPost(this, HttpInterface.URL_ROLE_EDIT, params, function (response) {
        var success = response.data.success;
        if (success) {
          _this.dialogFormVisible = false;
          _this.$parent.onRefreshClick();
        }
      }, null, true)
    },

    show(id) {
      this.dialogFormVisible = true;
      this.roleName = "";
      this.roleDescribe = "";
      if (id != 0) {
        this.id = id;
        this.title = "编辑角色信息";
        this.getRoleInfo(id);
      } else {
        this.title = "添加角色信息";
      }
    }
  }
};
</script>

<style  scoped>
.el_input {
  width: 230px;
}

.formCenter {
  display: flex;
  justify-content: center;
}
</style>
